package com.adnaan.restaurant.service.impl;

import com.adnaan.restaurant.common.untils.PageResult;
import com.adnaan.restaurant.common.untils.PageUtils;
import com.adnaan.restaurant.entity.CateGory;
import com.adnaan.restaurant.entity.Evaluation;
import com.adnaan.restaurant.entity.ResOrder;
import com.adnaan.restaurant.entity.ResShop;
import com.adnaan.restaurant.mapper.ResShopMapper;
import com.adnaan.restaurant.service.IResShopService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * author: adnaan
 * create: 2023-11-08 18:46
 * Description:
 */
@Service
public class ResShopServiceImpl implements IResShopService {
    @Autowired
    private ResShopMapper resShopMapper;

    /**
     * 查询菜品管理
     *
     * @param id 菜品管理主键
     * @return 菜品管理
     */
    @Override
    public ResShop selectResShopById(Long id) {
        return resShopMapper.selectResShopById(id);
    }

    /**
     * 查询菜品管理列表
     *
     * @return 菜品管理
     */
    @Override
    public PageResult selectResShopList(String shopName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ResShop> list = resShopMapper.selectResShopList(shopName);
        PageInfo<ResShop> resShopPageInfo = new PageInfo<>(list);
        PageResult pageResult = PageUtils.getPageResult(resShopPageInfo);
        return pageResult;
    }

    @Override
    public List<CateGory> selectCateGoryByShopId(Long shopId) {
        return resShopMapper.selectCateGoryByShopId(shopId);
    }

    @Override
    public List<ResShop> hotShop() {
        return resShopMapper.hotShop();
    }

    @Override
    @Transactional
    public int evaluate(Evaluation evaluation) {
        resShopMapper.evaluate(evaluation);
        double sum = resShopMapper.countStar();
        int totalStar = resShopMapper.sumStar(evaluation.getShopId());
        double result = totalStar / sum;
        return resShopMapper.reChangeStar(result, evaluation.getShopId());
    }

    @Override
    public PageResult evalList(Integer pageNum, Integer pageSize, Long shopId) {
        PageHelper.startPage(pageNum, pageSize);
        List<ResOrder> list = resShopMapper.evalList(shopId);
        PageInfo<ResOrder> orderPageInfo = new PageInfo<>(list);
        PageResult pageResult = PageUtils.getPageResult(orderPageInfo);
        return pageResult;
    }

}
